#include "qprogram.h"
#include "ui_qprogram.h"

QProgram::QProgram(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QProgram)
{
    ui->setupUi(this);



    //实例化界面
    myLogin = new Mylogin;
    myStm = new MyStm;
    myModbus = new MyModbus;
    myVideo = new VideoWin;
    //myHis = new MyHistory;
    myWindow = new MainWindow;
    myHomePage = new HomePage;
    data = new DataInfo;

    //将界面依次添加堆栈窗体
    ui->stackedWidget->addWidget(myLogin);
    ui->stackedWidget->addWidget(myHomePage);
    ui->stackedWidget->addWidget(myStm);
    ui->stackedWidget->addWidget(myModbus);
    ui->stackedWidget->addWidget(myVideo);
    ui->stackedWidget->addWidget(myWindow);


    connect(ui->listWidget,SIGNAL(currentRowChanged(int)),ui->stackedWidget,SLOT(setCurrentIndex(int)));

    connect(data,SIGNAL(keyValueOut(int,QString)),myHomePage,SLOT(keyValueGetSlot(int,QString)));
    connect(data,SIGNAL(keyValueOut(int,QString)),myStm,SLOT(keyValueGetSlot(int,QString)));
    connect(data,SIGNAL(keyValueOut(int,QString)),myModbus,SLOT(keyValueGetSlot(int,QString)));
    connect(myLogin,SIGNAL(getConnectIp(QString)),this,SLOT(getConnectIpSlot(QString)));
    connect(data,SIGNAL(hisTemSignal(int,QString)),myWindow,SLOT(getHisTemData(int,QString)));
    connect(data,SIGNAL(hisHumSignal(int,QString)),myWindow,SLOT(getHisHumData(int,QString)));
}


void QProgram::dataUpSlot(QByteArray msg)
{
    data->inparse(msg);
}

void QProgram::getConnectIpSlot(QString ipp)
{
    //mqtt相关
    mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress(ipp));
    mqtt->setPort(1883);
    mqtt->connectToHost();
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),this, SLOT(dataUpSlot(QByteArray)));//哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
}

QProgram::~QProgram()
{
    delete ui;
}



void QProgram::paintEvent(QPaintEvent *event)
{
    //创建画家类对象
    QPainter painter(this);
    QPixmap map(":/new/prefix1/res/1.JPG");
    painter.drawPixmap(0,0,this->width(),this->height(),map);
}
